package com.mowanka.mokeng.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.mowanka.mokeng.app.network.apiService
import com.mowanka.mokeng.app.network.stateCallback.ListDataUiState
import com.mowanka.mokeng.data.model.bean.CommentProduct
import com.mowanka.mokeng.data.model.bean.format
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

class RequestTradingViewModel : BaseViewModel() {
    private var pageNo = 1
    private var pageSize = 15
    var productList = MutableLiveData<ListDataUiState<CommentProduct>>()
    fun getProductList(map: MutableMap<String, Any>, isRefresh: Boolean = true) {
        if (isRefresh) {
            pageNo = 1
        }
        map["pageNumber"] = pageNo
        map["pageSize"] = pageSize
        request({ apiService.productList(map) }, {
            productList.value = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                hasMore = it.size >= pageSize,
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.format()
            )
        }, {
            productList.value = ListDataUiState(
                isSuccess = false,
                errMessage = it.errorMsg,
                isRefresh = isRefresh,
                listData = arrayListOf()
            )
        })
    }
}